﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Infrastructure.CompactCE.ExceptionHandling;
using SkynetCE.Foundation.Services.Navigation;
using SkynetCE.Foundation.Services.DatabaseService;
using System.Data.SqlServerCe;
using SkynetCE.Foundation.Model;

namespace SkynetCE.Administration.Views.MgmCategoriesMerge
{
    public class MgmCategoriesPresenter : Foundation.Views.Master.MasterPresenter<IMgmCategoriesView>
    {
        public IDatabaseService DatabaseService { protected get; set; }

        public MgmCategoriesPresenter()
        {
            this.DatabaseService = NavigationService.ResolveService<IDatabaseService>();
        }

        public override void OnViewReady()
        {
            this.LoadCategories();  
        }
        public void LoadCategories()
        {
            SqlCeConnection sql = null;
            SqlCeDataReader reader = null;
            var result = new List<Foundation.Model.ExamCategory>();
            try
            {
                var cs = this.DatabaseService.MergeAppConnectionString;
                sql = new SqlCeConnection(cs);
                var cmd = new SqlCeCommand("select id, title, description from  ExamCategories", sql);
                sql.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var ent = new Foundation.Model.ExamCategory();
                    ent.Id = reader.GetGuid(0);
                    ent.Title = reader.GetString(1);
                    ent.Description = reader.GetString(2);
                    result.Add(ent);
                }
                this.View.Categories = result;
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.CompactCE.ExceptionHandling.ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw;
            }
            finally {
                if (reader != null) reader.Close();  
                if (sql != null) sql.Close();  
            }            
        }

        internal void CreateCategory(ExamCategory ent)
        {
            SqlCeConnection sql = null;
            
            try
            {
                var cs = this.DatabaseService.MergeAppConnectionString;
                sql = new SqlCeConnection(cs);
                var cmd = new SqlCeCommand("insert into ExamCategories (id, title, description) values(@id,@title,@description)", sql);
                cmd.Parameters.AddWithValue("@id", ent.Id);
                cmd.Parameters.AddWithValue("@title", ent.Title);
                cmd.Parameters.AddWithValue("@description", ent.Description);
                sql.Open();
                cmd.ExecuteNonQuery();
               
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.CompactCE.ExceptionHandling.ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw;
            }
            finally
            {            
                if (sql != null) sql.Close();
            } 
        }

        internal void SyncData()
        {
            var repl = new SqlCeReplication();

            repl.InternetUrl = @"http://devmobile/SkynetMerge/sqlcesa35.dll";
            repl.InternetLogin = @"DEVMOBILE\Developer";
            repl.InternetPassword = "Pa$$w0rd";

            repl.Publisher = @"DEVMOBILE";
            repl.PublisherDatabase = @"SkynetLibraryRep";
            repl.PublisherSecurityMode = SecurityType.NTAuthentication;
            //repl.PublisherLogin = @"sa";
            //repl.PublisherPassword = @"<...>";

            repl.ExchangeType = ExchangeType.BiDirectional;
            repl.Publication = @"MOBILEMERGEREP";
            repl.Subscriber = @"MOBILESUSCRIPTION";


            repl.SubscriberConnectionString = this.DatabaseService.MergeAppConnectionString;
            try
            {

                //repl.AddSubscription(AddOption.ExistingDatabase);
                repl.Synchronize();
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.CompactCE.ExceptionHandling.ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw;
            }
        }
    }
}
